<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/18
 * Time: 上午10:07
 */

namespace App\Admin\Controllers;

use App\Models\Banner;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use Encore\Admin\Controllers\ModelForm;
use App\Http\Controllers\Controller;

class BannerController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header('轮播列表');
            $content->description('轮播列表');
            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('轮播详情');
            $content->description('');
            $content->body($this->form('edit')->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {
            $content->header('添加轮播');
            $content->description('');
            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid() {
        return Admin::grid(Banner::class, function (Grid $grid) {
            $grid->id('ID')->sortable();
            $grid->platform('所属平台')->display(function($platform){
                if( $platform==2 ) return '移动端';
                if( $platform==1 ) return 'PC';
                return '通用';
            });
            $grid->column('title', '标题');
            $grid->poster('主图')->display(function($img){
                return "<img src='".config('admin.upload.host')."{$img}' width='200' height='120'/>";
            });
            $grid->desc('Banner描述');
            $grid->show_order('展示顺序');
            $grid->flag('是否生效')->display(function($flag){
                if( $flag==1 ) return '有效';
                return '无效';
            });
            $grid->created_at('添加时间');
            $grid->updated_at('更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($mode='')
    {
        return Admin::form(Banner::class, function (Form $form) use($mode){
            if($mode=='edit'){
                $form->display('id','ID');
                $form->display('created_at', '创建时间');
                $form->display('updated_at', '更新时间');
            }
            $form->select('platform', '所属平台')->options(['0'=>'通用',2=>'移动端', 1=>'PC']);

            $form->text('title','标题');
            $form->text('desc','描述');
            $form->image('poster', '海报')->uniqueName()->removable();
            $form->select('open_type', '点击类型')->options(function(){
                return [
                    'link'      =>  '普通外链',
                    //'video_list'=>  '视频列表内页',
                    'inner_id'=>  '内部跳转',
                ];
            });
            $form->text('type_info', '点击内容（外链填地址，内部跳转ID）');
            $form->text('show_order','展示顺序,越小越靠前');
            $form->select('flag', '是否生效')->options([0=>'否', 1=>'是']);

        });
    }
}